/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.his.recordsCenter.service.impl;

import me.zhengjie.modules.his.domain.Type;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.his.recordsCenter.repository.TypeRepository;
import me.zhengjie.modules.his.recordsCenter.service.TypeService;
import me.zhengjie.modules.his.recordsCenter.service.dto.TypeDto;
import me.zhengjie.modules.his.recordsCenter.service.dto.TypeQueryCriteria;
import me.zhengjie.modules.his.recordsCenter.service.mapstruct.TypeMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author his
* @date 2022-11-11
**/
@Service
@RequiredArgsConstructor
public class TypeServiceImpl implements TypeService {

    private final TypeRepository typeRepository;
    private final TypeMapper typeMapper;

    @Override
    public Map<String,Object> queryAll(TypeQueryCriteria criteria, Pageable pageable){
        Page<Type> page = typeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(typeMapper::toDto));
    }

    @Override
    public List<TypeDto> queryAll(TypeQueryCriteria criteria){
        return typeMapper.toDto(typeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public TypeDto findById(Integer typeId) {
        Type type = typeRepository.findById(typeId).orElseGet(Type::new);
        ValidationUtil.isNull(type.getTypeId(),"Type","typeId",typeId);
        return typeMapper.toDto(type);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TypeDto create(Type resources) {
        return typeMapper.toDto(typeRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Type resources) {
        Type type = typeRepository.findById(resources.getTypeId()).orElseGet(Type::new);
        ValidationUtil.isNull( type.getTypeId(),"Type","id",resources.getTypeId());
        type.copy(resources);
        typeRepository.save(type);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer typeId : ids) {
            typeRepository.deleteById(typeId);
        }
    }

    @Override
    public void download(List<TypeDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TypeDto type : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("药品类型", type.getTypeName());
            map.put("创建者", type.getCreateBy());
            map.put("更新者", type.getUpdateBy());
            map.put("创建日期", type.getCreateTime());
            map.put("更新时间", type.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}